package com.ailink.basic.proxy;

import org.springframework.cglib.proxy.*;

import java.lang.reflect.Method;

public class TestCglib {
    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(Car.class);
        enhancer.setCallbacks(new Callback[] {
                new MethodInterceptor() {
                    @Override
                    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                        System.out.println("before");
                        Object response = methodProxy.invokeSuper(o, objects);
                        System.out.println("after");
                        return response;
                    }
                },
                NoOp.INSTANCE
        });
        enhancer.setCallbackFilter(new CallbackFilter() {
            @Override
            public int accept(Method method) {
                if (method.getName().contains("method")) {
                    return 0;
                }
                return 1;
            }
        });
        Car car = (Car) enhancer.create();
        car.method();
        car.drive();
    }
}
